from flask import request
from . import sql
from common.mysql import get_mysql_data_l as get_mysql_data
from common.mysql import post_mysql
from .sql_dict import sql_dict 


@sql.route('/',methods=['GET','POST'])
def _sql():
    if request.method == 'POST' and len(request.form)>0:
        sql = request.form.get('sql')
        if len(sql)<8:
            sql = sql_dict.get('本科')
    else:
        sql = sql_dict.get('本科')
    datas = get_mysql_data(sql)
    data1 = datas[0]
    data2 = [[str(item) for item in data] for data in datas[-1]]
    datas = [data1,data2]
    # datas = json.dumps(datas) 
    return datas

@sql.route('/post',methods=['GET','POST'])
def _post():
    if request.method == 'POST' and len(request.form)>0:
        try:
            post_mysql(request.form.get('sql'))
        except Exception as e:
            print(e)
    return ''